package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 29143
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id查询用户信息
     * @param uid
     * @return
     */
    @Override
    public Result findUserById(Integer uid) {
        User user = userMapper.selectById(uid);
        if (user!=null){
            return new Result(true,"查询成功",user);
        }
        return new Result(false,"查询失败",null);
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public Result findAll() {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
        List<User> users = userMapper.selectList(wrapper);
        if (users!=null&&users.size()>0){
            return new Result(true,"查询成功",users);
        }
        return new Result(false,"查询失败",null);
    }

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @Override
    public Result addUser(User user) {
        int insert = userMapper.insert(user);
        if (insert>0){
            return new Result(true,"添加成功",insert);
        }
        return new Result(false,"添加失败",null);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public Result updateUser(User user) {
        int update = userMapper.updateById(user);
        if (update>0) {
            return new Result(true,"修改成功",update);
        }
        return new Result(false,"修改失败",null);
    }

    /**
     * 删除用户信息
     * @param uid
     * @return
     */
    @Override
    public Result deleteUserByUid(Integer uid) {
        int i = userMapper.deleteById(uid);
        if (i>0) {
            return new Result(false,"删除成功",i);
        }
        return new Result(false,"删除失败",null);
    }

    /**
     * 分页查询用户信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result findUserByPage(Integer page, Integer size) {
        if (page==null) {
            page=2;
        }
        if (size==null) {
            size=2;
        }
        IPage<User> userIPage=new Page<>(page,size);
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
        IPage<User> userIPages = userMapper.selectPage(userIPage, wrapper);
        if (userIPages!=null&&userIPages.getTotal()>0) {
            return new Result(true,"查询成功",userIPages);
        }
        return new Result(false,"查询失败",null);
    }



    /*    *//**
     * 根据id查询用户信息
     * @param uid
     * @return
     *//*
    @Override
    public User findUserById(Integer uid) {
        User user = userMapper.findUserById(uid);
        return user;
    }

    *//**
     * 查询所有用户信息
     * @return
     *//*
    @Override
    public List<User> findAll() {
        List<User> all = userMapper.findAll();
        if (all!=null&&all.size()>0) {
            return all;
        }
        return null;
    }

    *//**
     * 添加用户信息
     * @param user
     * @return
     *//*
    @Override
    public Boolean addUser(User user) {
        Integer result = userMapper.addUser(user);
        if (result!=null&&result>0){
            return true;
        }
        return false;
    }

    *//**
     * 修改用户信息
     * @param user
     * @return
     *//*
    @Override
    public Boolean updateUser(User user) {
        Integer result = userMapper.updateUser(user);
        if (result!=null&&result>0) {
            return true;
        }
        return false;
    }

    *//**
     * 删除用户信息
     * @param uid
     * @return
     *//*
    @Override
    public Boolean deleteUserByUid(Integer uid) {
        Integer result = userMapper.deleteUserById(uid);
        if (result!=null&&result>0) {
            return true;
        }
        return false;
    }

    *//**
     * 分页查询用户
     * @param page
     * @param size
     * @return
     *//*
    @Override
    public IPage<User> findUserByPage(Integer page, Integer size) {
        //构建分页参数
        IPage<User> userPage = new Page<>(page, size);
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class);
        IPage<User> userIPage=userMapper.selectPage(userPage, wrapper);
        return userIPage;

    }*/
}
